﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechnicalIndicators.Points;
using Utils;

namespace TechnicalIndicators.DateBased
{
	public class RatioOfChange : GeneratorBase<DatedSeriePoint, DatedSeriePoint>
	{
		#region Attributes

		private int _period;
		private bool _absoluteMode;
		private readonly List<decimal> _values;

		#endregion

		#region Public Methods

		public RatioOfChange()
		{
			_values = new List<decimal>();
		}

		public RatioOfChange(ObservableCollection2<DatedSeriePoint> destination) : base(destination)
		{
			_values = new List<decimal>();
		}

		public void Configure(int period, bool absoluteMode)
		{
			_period = period;
			_absoluteMode = absoluteMode;
		}

		protected override DatedSeriePoint CreatePoint(DatedSeriePoint sourcePoint)
		{
			DatedSeriePoint result = null;

			if (_values.Count < _period)
			{
				_values.Add(sourcePoint.Y);
			}
			else
			{
				result = new DatedSeriePoint(sourcePoint.X, _absoluteMode ?  sourcePoint.Y - _values[0] : sourcePoint.Y * 100 / _values[0] - 100);
				_values.RemoveAt(0);
				_values.Add(sourcePoint.Y);
			}

			return result;
		}

		public override void Clear()
		{
			_values.Clear();

			base.Clear();
		}
		#endregion
	}
}
